//获取url信息
export const getUrl = (url)=>{
    let str = url.substr(1);
    let strs = str.split("&");
    let obj = {};
    for(let i = 0; i < strs.length; i ++)  
    {
        obj[strs[i].split("=")[0]] = strs[i].split("=")[1]
    }
    return obj;
};

export const getParams = (url)=>{
    //let str = url.substr(0);
    if(!url){
        return {};
    }
    let strs = url.split("&");
    let obj = {};
    for(let i = 0; i < strs.length; i ++)  
    {
        obj[strs[i].split("=")[0]] = strs[i].split("=")[1]
    }
    return obj;
};

export const getQueryURI = (url,obj,oldObj)=>{
    //合并对象
    let newObj = Object.assign({},oldObj,obj);

    let dataStr = '' //数据拼接字符串
    Object.keys(newObj).forEach(key => {
        if(key!=='code' && key!=='state'){
            dataStr += key + '=' + newObj[key] + '&'
        }
    })
    if (dataStr !== '') {
        dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
        url = url + '?' + dataStr
    }

    return url;
};

export const getEncodeURI = (url,obj)=>{
    let dataStr = '' //数据拼接字符串
    Object.keys(obj).forEach(key => {
        if(key!=='code' && key!=='state'){
            dataStr += key + '=' + obj[key] + '&'
        }
    })
    if (dataStr !== '') {
        dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
        url = url + '?' + dataStr
    }

    return encodeURIComponent(url);
};
//数组中重复的次数
export const arrayToCount = (arr,num = 1)=>{
    let obj = arr.reduce(function(prev,next){ 
        prev[next] = (prev[next] + 1) || 1; 
        return prev; 
    },{});
    //let newObj = {};
    let newArr = [];
    for(let name in obj){
        if(obj[name]>=num){
            //newObj[name] = obj[name];
            newArr.push(name)
        }
    }
    return newArr;
};

// 数组合并去重
export const diffarr = (arr1,arr2)=>{
    let set1  = new Set(arr1);
    let set2  = new Set(arr2);
    return [...new Set([...set1].filter(x => !set2.has(x)))]
}

// 检查数组或者字符串不能为空
export const checkArrStr = (name)=>{
    let arrBool = Array.isArray(name) && name.length>0;
    let strBool = !Array.isArray(name) && !emptyStr(name+"");

    if(name && (arrBool || strBool) ){
        return true
    }else{
        return false
    }
}

//解决微信ios上不回弹
export const wxBlur = ()=>{
    let myFunction
    let isWXAndIos = isWeiXinAndIos()
    if (isWXAndIos) {
        clearTimeout(myFunction)
        myFunction = setTimeout(function() {
            window.scrollTo({top: 0, left: 0, behavior: 'smooth'})
        }, 200)
    }
}
export const wxFocus = ()=>{
    let myFunction
    let isWXAndIos = isWeiXinAndIos()
    if (isWXAndIos) {
        clearTimeout(myFunction)
        myFunction = setTimeout(function() {
            window.scrollTo({top: 0, left: 0, behavior: 'smooth'})
            window.scrollTo({top:document.documentElement.clientHeight, left: 0,behavior: 'smooth'});
        }, 200)
    }
}
/*
export const isWeiXinAndIos = () => {
    let ua = '' + window.navigator.userAgent.toLowerCase()
    let isWeixin = /MicroMessenger/i.test(ua)
    let isIos = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(ua)
    return isWeixin && isIos
}*/

export const isWeiXinAndIos = ()=>{
    let ua = navigator.userAgent.toLowerCase();
    //Android终端
    /*
    let isAndroid1 = ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1;
    let isAndroid2 = /(Android)/i.test(navigator.userAgent);
    let isAndroid = isAndroid1 || isAndroid2
    */
    //Ios终端
    let isiOS1 = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
    let isiOS2 = /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent); 
    let isIos = isiOS1 || isiOS2
    //微信
    let isWeixin = /MicroMessenger/i.test(ua)
    return isWeixin && isIos
}

//空字符串检查
export const emptyStr = (str)=>{
    if(str==null || str.trim() === ""){
        return true
    }else{
        return false
    }
}
//手机号码判断
export const checkPhone = (val)=>{
    let sMobile = val;
    if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(sMobile))){ 
        return false; 
    }else{
        return true;
    }
}

//计算入学年份
export const study_year= (g)=>{
    let myDate = new Date();
    let year = myDate.getFullYear();
    let month = myDate.getMonth();

    let num = 1;
    if(month>=8){
        num = 0;
    }
    switch (g) {
        case "小班":
        case "一年级":
            break;
        case "中班":
        case "二年级":
            num = num + 1;
            break;
        case "大班":
        case "三年级":
            num = num + 2;
            break;
        case "四年级":
            num = num + 3;
            break;
        case "五年级":
            num = num + 4;
            break;
        case "六年级":
            num = num + 5;
            break;
    
        default:
            break;
    }
    return year - num;
}
//添加class
export const addClass = (el, className)=>{
    if (hasClass(el, className)) {
        return
    }

    let newClass = el.className.split(' ')
    newClass.push(className)
    el.className = newClass.join(' ')
}
export const hasClass=(el, className)=>{
    let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
    return reg.test(el.className)
}

//添加css
export const addCss = (href) => {
    const link = document.createElement('link')
    link.setAttribute('rel', 'stylesheet')
    link.setAttribute('typle', 'text/css')
    link.setAttribute('href', href)
    document.getElementsByTagName('head')[0].appendChild(link)
}
//移除css
export const removeCss = (href) => {
    const links = document.getElementsByTagName('link')
    for (let i = links.length; i >=0; i--) {
        const link = links[i]
        if(link && link.getAttribute('href') && link.getAttribute('href') === href){
            link.parentNode.removeChild(link)
        }
    }
}
//移除全部css
export const removeAllCss = (hrefs) => {
    for (let i = 0; i < hrefs.length; i++) {
        removeCss(hrefs[i])
    }
}

//检查是否是网址
export const CheckUrl = (url)=>{
    var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~/])+$/;
    if(!reg.test(url)){
        return false
    }else{
        return true
    }
}

// 秒转时分秒
export const secondToTime = (num)=>{
    let t = parseInt(num / (3600*24))
    let h = parseInt((num%(3600*24)) / 3600)
    let m = parseInt(((num%(3600*24)) % 3600) / 60)
    let s = parseInt(num % 60)

    let time = (t > 0 ? t + " 天 ":"")
                + (h > 0 ? h + " 小时 " : "")
                + (m > 0 ? m + " 分 " : "")
                + (s + " 秒")
    return time;
}

//订单号算法
export const createOrderId = ()=>{
    let myDate  = new Date();
    let month   = myDate.getMonth()+1;
    let date    = myDate.getDate();
    let hour    = myDate.getHours();
    let minute  = myDate.getMinutes();
    let second  = myDate.getSeconds();
    if (month < 10) {
        month = '0' + month;
    }
    if (date < 10) {
        date = '0' + date;
    }
    if (hour < 10) {
        hour = '0' + hour;
    }
    if (minute < 10) {
        minute = '0' + minute;
    }
    if (second < 10) {
        second = '0' + second;
    }
    return ""+myDate.getFullYear()+
            (Math.floor(Math.random()*90)+10)+
            month+
            (Math.floor(Math.random()*90)+10)+
            date+
            (Math.floor(Math.random()*90)+10)+
            hour+
            minute+
            second+
            (Math.floor(Math.random()*900)+100);
}